﻿using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Summer.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    public class PermissionRequireMent: IAuthorizationRequirement
    {
    }
    public class PermissionHandler : IAuthorizationHandler //AuthorizationHandler<PermissionRequireMent>
    {
        public Task HandleAsync(AuthorizationHandlerContext context)
        {
            var pendingRequirements = context.PendingRequirements.ToList();
            foreach (var requirement in pendingRequirements)
            {
                if (requirement is PermissionRequireMent)
                {
                    if (context.User.HasClaim(c => c.Type == "testpollyclaim" && c.Value == "polly1"))
                    {
                        context.Succeed(requirement);
                    }
                }
            }

            //if (context.User.HasClaim(c => c.Type == "testpollyclaim" && c.Value == "polly1"))
            //{
            //    context.Succeed(requirement);
            //}
            return Task.CompletedTask;
        }
        //protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
        //                                               PermissionRequireMent requirement)
        //{
        //    //if(context.User.HasClaim(c=>c.Type== "testpollyclaim"))
        //    //{
        //    //    context.Succeed(requirement);
        //    //}
        //    if (context.User.HasClaim(c => c.Type == "testpollyclaim" && c.Value == "polly2"))
        //    {
        //        context.Succeed(requirement);
        //    }
        //    return Task.CompletedTask;
        //}
    }
}
